// pages/enterprise/enterprise.js
import {
    getStaffLogOffList,
    staffLogOff
} from '../../api/app.js'
import user from '../../utils/user'
import { shake } from '../../utils/util'
const app = getApp()
Page({
    /**
	 * 页面的初始数据
	 */
    data: {
        isPopup:false,
        name:'',
        code:'',
        searchText:'',
        list:[],
        pageNum:1,
        pageSize:20,
        isPageNext:true
    },

    /**
	 * 生命周期函数--监听页面加载
	 */
    onLoad(options) {
        console.log(options.searchOpenWesure,'options.searchOpenWesure')
        this.getStaffLogOffList()
    },
    onShow(){
        // user.waitLogin().then(()=>{})
    },
    onReachBottom () {
        if(!this.data.isPageNext){
            return
        }
        this.setData({
            pageNum:this.data.pageNum+=1
        })
        this.getStaffLogOffList()
    },
    onIsEliminate(){
        this.setData({
            isPopup:true
        })
    },
    async getStaffLogOffList(){
        const result = await getStaffLogOffList({
            searchText:this.data.searchText,
            pageNum:this.data.pageNum,
            pageSize:this.data.pageSize
        })
        if(!result.hasNextPage){
            this.setData({
                isPageNext:false
            })
        }
        const arr = []
        arr.push(...this.data.list)
        arr.push(...result.list)
        this.setData({
            list:arr
        })
        console.log(this.data.list, 'result')
    },
    async onEliminate(){
        const isCode = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
        if(!this.data.name){
            app.toast('请输入姓名')
            return
        }
        if(!this.data.code){
            app.toast('请输入身份证')
            return
        }
        if(!isCode.test(this.data.code)){
            app.toast('请输入正确的身份证')
            return
        }
        await this.onStaffLogOff()
        this.setData({
            isPopup:false,
            name:'',
            code:''
        })
        await this.search()
    },
    async onStaffLogOff(){
        await staffLogOff({
            userName:this.data.name,
            idCardNo:this.data.code
        })
    },
    search(){
        shake(()=>{
            this.setData({
                list:[],
                pageNum:1,
                isPageNext:true
            })
            this.getStaffLogOffList()
        })
        console.log(this.data.searchText, 'searchText')
    },
    close(){
        this.setData({
            isPopup:false
        })
    }
})
